﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Practices.Unity;

namespace UnityStudy
{
    class Program
    {
        static void Main(string[] args)
        {
            IUnityContainer container = new UnityContainer();
            container.RegisterInstance<ILogger>(new FlatFileLogger());
            container.RegisterInstance<ILogger>("database", new DataBaseLogger());
            container.RegisterInstance<ILogger>("file",new FlatFileLogger());

            //1.非命名注册对象的获取
            ILogger Logger = container.Resolve<ILogger>();
            Logger.Log("QingdaoSoftPark");

            //2.命名对象的获取
            Logger = container.Resolve<ILogger>("database");
            Logger.Log("Beijing");

            //3.获取所有的对象（运行后如下，第一个没有提供名称的类型实例不会被获取到）
            IEnumerable<ILogger> all = container.ResolveAll<ILogger>();
            Console.WriteLine("容器内的对象分别为：");
            int logCount = 0;
            foreach (ILogger logger in all)
            {
                logCount++;
                logger.Log(logger.GetType().ToString());
            }

            Console.WriteLine(string.Format("共有{0}个注册对象", logCount));
            Console.ReadLine();
        }
    }
}
